<?php 
include_once "../../Lib/DBMySql.inc";
include_once "../../Lib/Common.php";
include_once "../../Lib/Lib.php";
include_once "../../Lib/Docs.php";
include_once "../../Entities/docsObj.php";
include_once "../../DAL/docsDAL.php";
include_once "../../Entities/productsObj.php";
include_once "../../DAL/productsDAL.php";
include_once "../../Lib/Content.php";
include_once "../../Lib/Session.php";
include_once "../header.php";

try
{
	$mainDB = NewDB(DATABASE_HOST, DATABASE_NAME , DATABASE_USER, DATABASE_PASSWORD);
	if (!isset($txtID) || $txtID=="")	$title		= "Add new product";
	else	$title		= "Edit product information";
	$pageBack = "list.php";
	$frmName = "frmMain";
	$pageError	= "";
	
	$uploadPath = "uploadfiles/";

	if (isset($act) && $act=="DELETE" && isset($deletedFile) && $deletedFile != "") {
		//DeleteFile($uploadPath.$deletedFile);
		DeleteFromDocs($deletedFile);
		$products = productsDAL::Load($mainDB, $txtID, $pageError);
		if ($products->imageID==$deletedFile)
			$products->imageID = "";
		$imageID = "";

		productsDAL::Update($mainDB, $products, $pageError);
	}

	if (isset($act) && ($act=="ADD" ||$act=="EDIT")) {
		if ($act=="EDIT") $editFilter = "AND id<>$txtID";
		
	}
	if ($pageError=="") 
		$fimageID = UploadFileToDocs("imageID", $uploadPath, IMAGE_FILE_TYPES, $pageError);
	if ($pageError!="") 
		$imageID = $oldimageID;

	if ($pageError=="")
	{
		if (isset($act) && ($act=="ADD" ||$act=="EDIT")) {
			if ($act=="ADD") {
				$products = new productsObj();
				productsDAL::Pop($products);
				if ($fimageID!="")
					$products->imageID = $fimageID;

				
				
				if (!productsDAL::Insert($mainDB, $products, $error)) 
					$pageError = "Invalid inserting. ($error).";
				else 
					$exit = true;
			} else if ($act=="EDIT") {
				$products = new productsObj();
				$products = productsDAL::Load($mainDB, $txtID, $pageError);
				$imageIDPath = $products->imageID;

				productsDAL::Pop($products);
				if ($fimageID!="") 
					$products->imageID = $fimageID; 
										
				else	
					$products->imageID = $imageIDPath;
				
				
				$products->id = $txtID;
				if (!productsDAL::Update($mainDB, $products, $error)) 
					$pageError = "Invalid updating. ($error).";
				else 
					$exit = true;
			}
		}
	}
	if (isset($txtID) && $txtID!="" && !isset($code)) {
		$products = productsDAL::Load($mainDB, $txtID, $pageError);
		if ($pageError=="" && $products!=null) {
			productsDAL::Push($products);
		}
	}
} 
catch(Exception $ex)
{ $pageError = $ex.message;}

?>
<form name=<?php  echo $frmName;?> method=post action="<?php  echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
<input type=hidden name=page value="<?php echo $page;?>">
<input type=hidden name=act value="<?php  if (!isset($txtID) || $txtID=="") echo "ADD"; else echo "EDIT";?>">
<input type=hidden name=txtID value="<?php  echo $txtID;?>">
<input type=hidden name=SortField value="<?php echo $SortField;?>">
<input type=hidden name=SortType value="<?php echo $SortType;?>">
<input type=hidden name="deletedFile">
<script>
<?php  if (isset($exit)) echo "document.$frmName.action = '$pageBack'; document.$frmName.submit();";?>
function ValidSubmit() {
	frm = document.<?php  echo $frmName;?>;
	
	if (!_CF_hasValue(frm.code, "TEXT")) {
		alert("Please enter product code");
		frm.code.focus();
		return false;
	}

	if (!_CF_hasValue(frm.name, "TEXT")) {
		alert("Please enter product name");
		frm.name.focus();
		return false;
	}

	if (frm.unitID.value=='' || frm.unitID.value=='0') {
		alert("Please select unit");
		frm.unitID.focus();
		return false;
	}

	if (frm.cateID.value=='' || frm.cateID.value=='0') {
		alert("Please select category");
		frm.cateID.focus();
		return false;
	}

	if (frm.subcateID.value=='' || frm.subcateID.value=='0') {
		alert("Please select sub category");
		frm.subcateID.focus();
		return false;
	}

	/*
	if (!_CF_hasValue(frm.Email, "TEXT")) {
		alert("Please enter your email address.");
		frm.Email.focus();
		return false;
	}
	else if (!isEmail(frm.Email.value)) {
		alert("Email address is invalid.");
		frm.Email.focus();
		return false;
	}
	// Check number field
	if (_CF_hasValue(frm.NumberField, "TEXT") && !_CF_checknumber(frm.NumberField.value)) {
		alert("Invalid date, you must input a numeric data.");
		frm.NumberField.focus();
		return false;
	}
	if (_CF_hasValue(frm.NumberField, "TEXT") && !_CF_checkinteger(frm.NumberField.value)) {
		alert("Invalid date, you must input a integer number data.");
		frm.NumberField.focus();
		return false;
	}
	*/
	return true;		
}

var comboCountry;

var countries = {};
countries.results = [
	{id:1,name:'Afghanistan'},
	{id:2,name:'Albania'}
];
countries.total = countries.results.length;

$(function() {
	// Default, when the number of results exceeds the paging threshold
	// Scrolling and watermark
	comboCountry = $('#ffb5').flexbox(countries, {
		watermark: 'Enter country name',
		initialValue: 'Albania',
		paging: false,
		maxVisibleRows: 8,
		onSelect: function() {  
			alert($('input[name=ffb5]').val());
		} 
	});

});
</script>
<table width="99%" border="0" cellspacing="0" cellpadding="0" align="right">
	<tr valign=middle> 
		<td class="titleText" colspan=3 height=25><?php  echo $title;?></td>
	</tr>
	<tr valign=middle> 
		<td colspan=3 height=3 bgcolor="#FFFFFF"></td>
	</tr>
	<tr> 
		<td align="center" valign="top" colspan=3>
			<table width="100%" border=0 cellspacing="0" class="formBackGround">
				<tr>
					<td>
						<TABLE border=0 class="gridBody" cellPadding=0 cellspacing="0" width="100%">
							<tr class="gridHeader">
								<td colspan=4 height=20 class='errortext'>&nbsp;<? echo $pageError;?>&nbsp;</td>
							</TR>
							<tr height=5>
								<td colspan=4 height=5>
                                	<b>FlexBox:</b> <div id="ffb5"></div>
                                </td>
							</TR>
							
							<tr class="gridBody" valign='top'>
								<td width="10" nowrap>&nbsp;</td>
								<td width="20%" nowrap>Product code<font color="#cc0000">*</font>:&nbsp;</td>
								<td width="10" nowrap>&nbsp;</td>
								<td width="80%" nowrap><input type=text name="code" value="<?php  echo $code;?>" size="70" maxlength="100" class="inputText">&nbsp;</td>
							</TR>

							<tr class="gridBody" valign='top'>
								<td width="10" nowrap>&nbsp;</td>
								<td width="20%" nowrap>Product name<font color="#cc0000">*</font>:&nbsp;</td>
								<td width="10" nowrap>&nbsp;</td>
								<td width="80%" nowrap><input type=text name="name" value="<?php  echo $name;?>" size="70" maxlength="255" class="inputText">&nbsp;</td>
							</TR>

							<tr class="gridBody" valign='top'>
								<td width="10" nowrap>&nbsp;</td>
								<td width="20%" nowrap>Description<font color="#cc0000"></font>:&nbsp;</td>
								<td width="10" nowrap>&nbsp;</td>
								<td width="80%" nowrap><textarea name="description" class="inputText" rows='7' cols='66'><?=$description;?></textarea>&nbsp;</td>
							</TR>

							<tr class="gridBody" valign='top'>
								<td width="10" nowrap>&nbsp;</td>
								<td width="20%" nowrap>Price<font color="#cc0000"></font>:&nbsp;</td>
								<td width="10" nowrap>&nbsp;</td>
								<td width="80%" nowrap><input type=text name="price" value="<?php  echo $price;?>" size="10" maxlength="20" class="inputText">&nbsp;</td>
							</TR>

							<tr class="gridBody" height=18>
								<td width="10" nowrap>&nbsp;</td>
								<td width="20%" nowrap>Unit<font color="#cc0000">*</font>:&nbsp;</td>
								<td width="10" nowrap>&nbsp;</td>
								<td width="80%" nowrap><?php  include_once "../../Entities/unitsObj.php"; include_once "../../DAL/unitsDAL.php"; echo GetComboBox($mainDB, unitsObj::TableName(), "", "", "unitID", "id", "name", "Select one", "", ${"unitID"}, "class='inputText'");?>&nbsp;</td>
							</TR>

							<tr class="gridBody" height=18>
								<td width="10" nowrap>&nbsp;</td>
								<td width="20%" nowrap>Category<font color="#cc0000">*</font>:&nbsp;</td>
								<td width="10" nowrap>&nbsp;</td>
								<td width="80%" nowrap><?php  include_once "../../Entities/categoriesObj.php"; include_once "../../DAL/categoriesDAL.php"; echo GetComboBox($mainDB, categoriesObj::TableName(), "", "", "cateID", "id", "name", "Select one", "", ${"cateID"}, "class='inputText'");?>&nbsp;</td>
							</TR>

							<tr class="gridBody" height=18>
								<td width="10" nowrap>&nbsp;</td>
								<td width="20%" nowrap>Sub category<font color="#cc0000">*</font>:&nbsp;</td>
								<td width="10" nowrap>&nbsp;</td>
								<td width="80%" nowrap><?php  include_once "../../Entities/subcatesObj.php"; include_once "../../DAL/subcatesDAL.php"; echo GetComboBox($mainDB, subcatesObj::TableName(), "", "", "subcateID", "id", "name", "Select one", "", ${"subcateID"}, "class='inputText'");?>&nbsp;</td>
							</TR>
							<tr class="gridBody">
								<td nowrap>&nbsp;</td>
								<td nowrap>Picture<font color="#cc0000"></font>:&nbsp;</td>
								<td nowrap>&nbsp;</td>
								<td nowrap>
									<input type=hidden name=oldimageID value="<?=(isset($oldimageID)?$oldimageID:$imageID)?>" />
                                	<input type=file class='inputText' name='imageID' size='72'>&nbsp;
                                    <?php
									if ($imageID!="" && $imageID!="0")
									{
										list($lpicture_x, $lpicture_y) = GetDocSizes($imageID); 
										$lpicture_x += 25;
										$lpicture_y += 35;
										echo "<a href=\"#\" title=\"View Image\" onClick=\"viewdoc($imageID, $lpicture_x, $lpicture_y); return false;\">View Image</a> | ";
										echo "<a href='#' onClick=\"_deleteFile('$imageID', document.$frmName);\">Delete</a>&nbsp;";
									}
									?>
                                </td>
							</TR>

							<?php  
								if (!isset($txtID) || $txtID=="")
									include "../../Lib/bottomNewAct.php";
								else 
									include "../../Lib/bottomUpdateAct.php";
							?>
							<tr class="gridFooter" height=20>
								<td colspan=4></td>
							</TR>
						</TABLE>
					</td>
				</tr>
			</TABLE>
		</td>
	</tr>
</table>
</form>
<script>
document.<?=$frmName;?>.code.focus();
</script>
<?php 
include "../footer.php";
?>